﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Serialization;
using System.IO;

namespace MyopicScheduler
{
    public class TaskSetGeneratorEntry
    {
        public static void Main(string[] args)
        {
            if (args.Length % 2 != 0 || args.Length <= 0)
            {
                Console.WriteLine("TaskSetGenerator Parameters:");
                Console.WriteLine("  -o [filename]");
                Console.WriteLine("     The path to the output XML file. Must not already exist.");
                Console.WriteLine("  -ns [NumSets]");
                Console.WriteLine("     The number of tasks sets to create in one file.");
                Console.WriteLine("  -np [NumProcs]");
                Console.WriteLine("     The number of processors to create tasks for.");
                Console.WriteLine("  -nr [NumResources] (optional, default 0)");
                Console.WriteLine("     The number of resources that tasks can depend on.");
                Console.WriteLine("  -t [Time]");
                Console.WriteLine("     The maximum length of the schedule.");
                Console.WriteLine("     The actual length will most likely be less.");
                Console.WriteLine("  -lc [MinC]");
                Console.WriteLine("     The minimum length of a task.");
                Console.WriteLine("     Values are selected randomly between this and the maximum.");
                Console.WriteLine("  -hc [MaxC]");
                Console.WriteLine("     The maximum length of a task.");
                Console.WriteLine("  -dv [DeadlineVariance]");
                Console.WriteLine("     The variance of task deadlines.");
                Console.WriteLine("     Deadlines are randomly extended by a value in [0, DV].");
                //Console.WriteLine("  -l [Laxity] (optional, default 0.0)");
                //Console.WriteLine("     The laxity of the deadlines.");
                //Console.WriteLine("     Deadlines are randomly extended by a value in [0, Laxity*SCT).");
                //Console.WriteLine("     (0 is the minimum and is a strict schedule)");
                Console.WriteLine("  -up [UseP] (optional, default 0.0)");
                Console.WriteLine("     The probability of a task using each resource in exclusive mode.");
                Console.WriteLine("  -sp [ShareP] (optional, default 0.0)");
                Console.WriteLine("     The probability of a task using each resource in shared mode.");
                Console.WriteLine("     (Only if not already in exclusive).");

                return;
            }
            else
            {
                string filename = null;
                for (int i = 0; i < args.Length; i += 2)
                {
                    switch (args[i])
                    {
                        case "-o":
                        case "--Output":
                            filename = args[i + 1];
                            break;
                    }
                }
                if (filename == null)
                {
                    throw new Exception("Missing parameter: Output");
                }

                var tsg = TaskSetGenerator.CreateGeneratorFromCLA(args);
                var data = tsg.GenerateTaskSets();
                WriteXmlFile(filename, data);
            }
        }

        // Unused
        static SimulationData Merge(SimulationData a, SimulationData b)
        {
            SimulationData c = new SimulationData();
            c.TaskSets.AddRange(a.TaskSets);
            c.TaskSets.AddRange(b.TaskSets);
            return c;
        }

        static void WriteXmlFile(string filename, SimulationData data)
        {
            try
            {
                FileStream file = new FileStream(filename, FileMode.CreateNew);
                XmlSerializer serial = new XmlSerializer(typeof(SimulationData));
                serial.Serialize(file, data);
                Console.WriteLine("File successfully written: " + filename);
            }
            catch (IOException)
            {
                Console.Error.WriteLine("I/O Error. Does the file already exist?");
            }
            catch (InvalidOperationException)
            {
                Console.Error.WriteLine("There was a problem serializing the file.");
            }
        }
    }
}
